package com.patent.common;

import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.patent.domain.vo.PatentFeeVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
* 发送短信
 **/
public class SmsUtil {

	private static final Logger log = LoggerFactory.getLogger(SmsUtil.class);

	/**
	 * 发送短信验证码
	 * @param userMobile 手机号
	 * @param activeTime 验证码有效时间
	 */
	public static Map<String,Object> sendMsg(String userMobile, String activeTime){
		Map<String,Object> map = new HashMap<String,Object>();
		String authCode = "";
		try {
			SmsSingleSender sender = new SmsSingleSender(PatentConstant.appId, PatentConstant.appKey);
			authCode = RandomUtil.getRandNum(6);
			ArrayList<String> params = new ArrayList<String>();
			params.add(authCode);
			params.add(activeTime);
			SmsSingleSenderResult result = sender.sendWithParam("86", userMobile, 101371, params, "", "", "");
			log.info("send result"+JsonUtil.write2JsonStr(result));
			if(result != null && result.result == 0){
				map.put("authCode",authCode);
			}else{
				authCode = "";
				map.put("authCode",authCode);
				map.put("errMsg",result.errMsg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error("SmsUtil sendMsg发送短信验证码失败"+e.getMessage());
		}
		return map;
	}

	/**
	 * 年费截止日前三个月，前一个月，前7天，各提醒一次。滞纳金 产生后提醒一次。
	 * @param patentFeeVo
	 */
	public static void sendRemindMessage(PatentFeeVo patentFeeVo){
		Map<String,Object> map = new HashMap<String,Object>();
		try {
			SmsSingleSender sender = new SmsSingleSender(PatentConstant.appId, PatentConstant.appKey);
			ArrayList<String> params = new ArrayList<String>();
			params.add(patentFeeVo.getTitle());
			Integer tempId = null;
			if(patentFeeVo.getDiffDay() != null && patentFeeVo.getDiffDay() > 1){//90 30 7
				params.add(String.valueOf(patentFeeVo.getDiffDay()));
				tempId = 115099;
			}else if(patentFeeVo.getDiffDay() != null && patentFeeVo.getDiffDay() == 1){ // 1
				Date dueDate = patentFeeVo.getDueDate();
				if(dueDate != null){
					params.add(DateUtil.formatDateToString(dueDate, new SimpleDateFormat("yyyy-MM-dd")));
				}
				tempId = 115100;
			}
			SmsSingleSenderResult result = sender.sendWithParam("86", patentFeeVo.getUserMobile(), tempId, params, "", "", "");
			log.info("send result"+JsonUtil.write2JsonStr(result));
			if(result != null && result.result == 0){
				log.info("发送成功");
			}else{
				log.error("errMsg" + result.errMsg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error("SmsUtil sendMsg发送年费监控提醒失败"+e.getMessage());
		}
	}

	/**
	 * 定时发送短信新接口
	 * @param userMobile
	 * @param count
	 * @param flag 周还是月
	 */
	public static void sendRemindMessageNew(String userMobile, int count, String flag) {
		Map<String,Object> map = new HashMap<String,Object>();
		try {
			SmsSingleSender sender = new SmsSingleSender(PatentConstant.appId, PatentConstant.appKey);
			ArrayList<String> params = new ArrayList<String>();
			params.add(String.valueOf(count));
			params.add(flag);
			Integer tempId = 158804;
			SmsSingleSenderResult result = sender.sendWithParam("86", userMobile, tempId, params, "", "", "");
			log.info("send result userMobile:" + userMobile + ":=="+JsonUtil.write2JsonStr(result));
			if(result != null && result.result == 0){
				log.info("发送成功");
			}else{
				log.error("errMsg" + result.errMsg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error("SmsUtil sendMsg发送年费监控提醒失败"+e.getMessage());
		}
	}
}
